package cn.yemuysy.code.lingting;

import cn.yemuysy.code.lingting.model.MatchResult;
import cn.yemuysy.code.lingting.service.SensitiveWordDetector;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 敏感词检测系统演示程序
 * 展示普通敏感词、组合敏感词、拼音敏感词等功能
 */
public class Main {
    
    private static final Logger logger = LoggerFactory.getLogger(Main.class);
    
    public static void main(String[] args) {
        System.out.println("=== 敏感词检测系统演示 ===");
        
        // 创建检测器实例
        SensitiveWordDetector detector = new SensitiveWordDetector();
        
        // 演示各种功能
        demonstrateNormalWords(detector);
        demonstrateCombinationWords(detector);
        demonstratePinyinWords(detector);
        demonstrateStatistics(detector);
        demonstratePerformance(detector);
        
        System.out.println("\n=== 演示完成 ===");
    }
    
    /**
     * 演示普通敏感词检测
     */
    private static void demonstrateNormalWords(SensitiveWordDetector detector) {
        System.out.println("\n--- 普通敏感词检测演示 ---");
        
        // 添加普通敏感词
        List<String> normalWords = Arrays.asList(
            "暴力", "色情", "赌博", "毒品", "反动", "邪教",
            "她", "他", "她们", "广告", "网站"
        );
        detector.addNormalWords(normalWords);
        
        // 测试文本
        List<String> testTexts = Arrays.asList(
            "这是一段正常的文本内容",
            "请远离暴力和色情内容",
            "他说她们不应该参与赌博活动",
            "反动言论和毒品都是危险的",
            "某些邪教组织会传播有害信息"
        );
        
        for (String text : testTexts) {
            List<MatchResult> results = detector.detectAll(text);
            System.out.printf("文本: %s\n", text);
            if (results.isEmpty()) {
                System.out.println("  无敏感词");
            } else {
                for (MatchResult result : results) {
                    System.out.printf("  敏感词: %s, 位置: %d-%d, 类型: %s\n", 
                                    result.getWord(), result.getStartIndex(), 
                                    result.getEndIndex(), result.getMatchType());
                }
            }
            System.out.println();
        }
    }
    
    /**
     * 演示组合敏感词检测
     */
    private static void demonstrateCombinationWords(SensitiveWordDetector detector) {
        System.out.println("\n--- 组合敏感词检测演示 ---");
        
        // 添加组合敏感词
        List<String> combinationWords = Arrays.asList(
            "澳门+博彩+网站",
            "赌博+广告",
            "暴力+游戏",
            "色情+内容+传播"
        );
        detector.addCombinationWords(combinationWords);
        
        // 测试文本
        List<String> testTexts = Arrays.asList(
            "欢迎登录澳门XX博彩官方网站",
            "这里有赌博相关的广告信息",
            "某些暴力类型的游戏不适合儿童",
            "禁止色情内容的传播和分享",
            "澳门是个旅游胜地，有很多网站介绍",
            "博彩业是当地的重要产业"
        );
        
        for (String text : testTexts) {
            List<MatchResult> results = detector.detectAll(text);
            System.out.printf("文本: %s\n", text);
            
            // 分别显示不同类型的匹配
            List<MatchResult> normalMatches = results.stream()
                    .filter(r -> r.getMatchType() == MatchResult.MatchType.NORMAL)
                    .collect(Collectors.toList());
            List<MatchResult> combinationMatches = results.stream()
                    .filter(r -> r.getMatchType() == MatchResult.MatchType.COMBINATION)
                    .collect(Collectors.toList());
            
            if (!normalMatches.isEmpty()) {
                System.out.println("  普通敏感词:");
                for (MatchResult result : normalMatches) {
                    System.out.printf("    %s (%d-%d)\n", 
                                    result.getWord(), result.getStartIndex(), result.getEndIndex());
                }
            }
            
            if (!combinationMatches.isEmpty()) {
                System.out.println("  组合敏感词:");
                for (MatchResult result : combinationMatches) {
                    System.out.printf("    %s (%d-%d) 组成: %s\n", 
                                    result.getWord(), result.getStartIndex(), result.getEndIndex(),
                                    result.getCombinationParts());
                }
            }
            
            if (normalMatches.isEmpty() && combinationMatches.isEmpty()) {
                System.out.println("  无敏感词");
            }
            System.out.println();
        }
    }
    
    /**
     * 演示拼音敏感词检测
     */
    private static void demonstratePinyinWords(SensitiveWordDetector detector) {
        System.out.println("\n--- 拼音敏感词检测演示 ---");
        
        // 添加拼音敏感词
        List<String> pinyinWords = Arrays.asList(
            "彩票", "啋票", "采漂",  // 谐音变体
            "博彩", "薄材", "博才",  // 谐音变体
            "赌博", "堵搏", "睹博"   // 谐音变体
        );
        detector.addPinyinWords(pinyinWords);
        
        // 测试文本
        List<String> testTexts = Arrays.asList(
            "购买彩票需要理性投注",
            "这个啋票活动很有趣",
            "他喜欢采漂这项运动",
            "博彩行业监管严格",
            "这位薄材的学者很有名",
            "他是个博才多艺的人",
            "远离赌博，珍爱生命",
            "不要堵搏在这里",
            "他睹博了一整夜"
        );
        
        for (String text : testTexts) {
            List<MatchResult> results = detector.detectAll(text);
            System.out.printf("文本: %s\n", text);
            
            List<MatchResult> pinyinMatches = results.stream()
                    .filter(r -> r.getMatchType() == MatchResult.MatchType.PINYIN)
                    .collect(Collectors.toList());
            
            if (!pinyinMatches.isEmpty()) {
                System.out.println("  拼音敏感词:");
                for (MatchResult result : pinyinMatches) {
                    System.out.printf("    %s (%d-%d)\n", 
                                    result.getWord(), result.getStartIndex(), result.getEndIndex());
                }
            } else {
                System.out.println("  无拼音敏感词");
            }
            System.out.println();
        }
    }
    
    /**
     * 演示统计信息
     */
    private static void demonstrateStatistics(SensitiveWordDetector detector) {
        System.out.println("\n--- 敏感词库统计信息 ---");
        
        Map<String, Integer> stats = detector.getStatistics();
        for (Map.Entry<String, Integer> entry : stats.entrySet()) {
            System.out.printf("%s: %d 个\n", entry.getKey(), entry.getValue());
        }
        
        System.out.println("\n检测器配置:");
        SensitiveWordDetector.DetectionConfig config = detector.getConfig();
        System.out.printf("普通敏感词检测: %s\n", config.isEnableNormal() ? "启用" : "禁用");
        System.out.printf("组合敏感词检测: %s\n", config.isEnableCombination() ? "启用" : "禁用");
        System.out.printf("拼音敏感词检测: %s\n", config.isEnablePinyin() ? "启用" : "禁用");
        System.out.printf("拼音组合检测: %s\n", config.isEnablePinyinCombination() ? "启用" : "禁用");
        System.out.printf("去重过滤: %s\n", config.isEnableDuplicateFilter() ? "启用" : "禁用");
        System.out.printf("最大结果数: %d\n", config.getMaxResults());
    }
    
    /**
     * 演示性能测试
     */
    private static void demonstratePerformance(SensitiveWordDetector detector) {
        System.out.println("\n--- 性能测试演示 ---");
        
        // 构造长文本进行性能测试
        StringBuilder longText = new StringBuilder();
        String baseText = "这是一段包含各种内容的长文本，可能包含暴力、色情、赌博等敏感词，" +
                         "也可能包含澳门博彩网站这样的组合敏感词，甚至还有啋票、薄材等拼音谐音词。";
        
        for (int i = 0; i < 100; i++) {
            longText.append(baseText).append("段落").append(i + 1).append("。");
        }
        
        String testText = longText.toString();
        
        // 性能测试
        long startTime = System.currentTimeMillis();
        List<MatchResult> results = detector.detectAll(testText);
        long endTime = System.currentTimeMillis();
        
        System.out.printf("文本长度: %d 字符\n", testText.length());
        System.out.printf("检测耗时: %d 毫秒\n", (endTime - startTime));
        System.out.printf("匹配结果: %d 个\n", results.size());
        System.out.printf("平均速度: %.2f 字符/毫秒\n", 
                         (double) testText.length() / (endTime - startTime));
        
        // 显示部分匹配结果
        System.out.println("\n部分匹配结果:");
        results.stream().limit(10).forEach(result -> 
            System.out.printf("  %s (%s) 位置: %d-%d\n", 
                            result.getWord(), result.getMatchType(), 
                            result.getStartIndex(), result.getEndIndex())
        );
        
        if (results.size() > 10) {
            System.out.printf("  ... 还有 %d 个结果\n", results.size() - 10);
        }
    }
} 